package com.duolebo.uteped_sdk.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * subclass this class to create your own database,
 *
 */
public class SampleDB extends SQLiteOpenHelper implements IDatabase {

    private Map<String, Table> tables = new HashMap<>();
    private int openedConnections = 0;

    /**
     * SampleDB
     * @param context context
     * @param name name
     * @param version version
     */
    public SampleDB(Context context, String name, int version) {
        super(context, name, null, version);
    }

    /**
     * putTable
     *
     * @param name  name
     * @param table table
     */
    public void putTable(String name, Table table) {
        if (!tables.containsKey(name)) {
            tables.put(name, table);
        }
    }

    public Table getTable(String name) {
        return tables.get(name);
    }

    @Override
    public synchronized IDatabase open() {
        openedConnections++;
        return this;
    }

    @Override
    public synchronized void close() {
        if (0 == --openedConnections)
            super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        for (Map.Entry<String, Table> entry : tables.entrySet()) {
            Table table = entry.getValue();
            String createClause = table.getTableCreateClause();
            if (null == createClause) {
                Log.d("SampleDB", "Empty CREATE clause.");
                continue;
            }
            db.execSQL(createClause);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // override this method when database schema has changed.
    }

}
